package com.example.demo.thread;

/**
 * 线程循环打印abc
 *
 * @author Code Farmer
 * @date 2020/5/28 22:58
 */
public class ABPrintDemo {

    private static volatile int flag = 1;

    public static void main(String[] args) throws InterruptedException {
        new Thread(() -> {
            for (; ; ) {
                if (flag == 1) {
                    System.out.print("a");
                    flag = 2;
                }
            }
        }).start();
        Thread.sleep(500);
        new Thread(() -> {
            for (; ; ) {
                if (flag == 2) {
                    System.out.print("b");
                    flag = 3;
                }
            }
        }).start();
        new Thread(() -> {
            for (; ; ) {
                if (flag == 3) {
                    System.out.print("c");
                    flag = 1;
                }
            }
        }).start();
    }

}
